const path = require("path");
const fse = require("fs-extra");
const gulp = require("gulp");

module.exports = function (options = {}) {
    if (!options || !options.buildpath) {
        throw new Error("need options buildpath");
    }
    if (!options.rootpath) {
        throw new Error("need options rootpath");
    }
    const yargs = require("yargs");
    const argv = yargs.argv;
    let type = "none";
    let templatepath = options.templatepath;
    if (argv.type) {
        if (argv.type === "make:component") {
            if (argv.name && argv.dest) {
                gulp.src(path.join(templatepath, "component/index.es7"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "component/index.wxsc"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "component/index.json5"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "component/index.axml"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
            }
        }


        if (argv.type === "make:page") {
            if (argv.name && argv.dest) {
                gulp.src(path.join(templatepath, "page/index.es7"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "page/index.wxsc"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "page/index.json5"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
                gulp.src(path.join(templatepath, "page/index.axml"))
                    .pipe(gulp.dest(path.join(options.rootpath,  argv.dest)))
            }
        }

        if (options.onType) {
            options.onType(argv, gulp, {});
        }
    }
};